import { FieldTypeName } from "../../excel/celltypes/FieldCellType";

export const getTextLength = (d) => {
    const dbtype = d.dbtype || '0';
    if (d.typeName === FieldTypeName.Text) {
        return parseInt(dbtype.substring(dbtype.indexOf('(') + 1));
    } else if(d.typeName === FieldTypeName.Int) {
        return 11;
    } else if(d.typeName === FieldTypeName.Float) {
        return parseInt(dbtype.substring(dbtype.indexOf('(') + 1, dbtype.indexOf(','))) - parseInt(dbtype.substring(dbtype.indexOf(',') + 1));;
    } else if(d.typeName === FieldTypeName.Thousand) {
        const length = parseInt(dbtype.substring(dbtype.indexOf('(') + 1))-3;
        return length - parseInt(''+length / 4);
    } else if(d.typeName === FieldTypeName.Money) {
        return parseInt(dbtype.substring(dbtype.indexOf('(') + 1, dbtype.indexOf(','))) - parseInt(dbtype.substring(dbtype.indexOf(',') + 1));;
    }
    return 0;
}

export const getFloatQfws = (d) => {
    const dbtype = d.dbtype || '0';
    if (d.typeName === FieldTypeName.Text) {
        return 0;
    } else if(d.typeName === FieldTypeName.Int) {
        return 0;
    } else if(d.typeName === FieldTypeName.Float) {
        return parseInt(dbtype.substring(dbtype.indexOf(',') + 1));
    } else if(d.typeName === FieldTypeName.Thousand) {
        return 2;
    } else if(d.typeName === FieldTypeName.Money) {
        return parseInt(dbtype.substring(dbtype.indexOf(',') + 1));
    }
    return 0;
}
export const getFloatLength = (dbtype: string) => {
    return parseInt(dbtype.substring(dbtype.indexOf('(') + 1, dbtype.indexOf(',')));
}
export const getRealLength = (str: string) => {
    ///<summary>获得字符串实际长度，中文2，英文1</summary>
    ///<param name="str">要获得长度的字符串</param>
    var realLength = 0, len = str.length, charCode = -1;
    for (var i = 0; i < len; i++) {
        charCode = str.charCodeAt(i);
        if (charCode >= 0 && charCode <= 128)
            realLength += 1;
        else
            realLength += 2;
    }
    return realLength;
};